﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using HandyControl.Controls;
using CommunityToolkit.Mvvm.Input;
using Ops.Host.Common;
using Ops.Host.Core.Management;
using Ops.Host.Core.Models;
using Ops.Host.Core.Services;

namespace Ops.Host.App.ViewModels;

public sealed class DictInfoViewModel : SinglePagedViewModelBase<DictInfo, DictInfoFilter>
{
    private readonly IDictInfoService _dictInfoService;

    public DictInfoViewModel(IDictInfoService dictInfoService)
    {
        _dictInfoService = dictInfoService;

        AddCommand = new RelayCommand(() => Add());
        UpdateCommand = new RelayCommand<DictInfo>((item) => Update(item!));
        DeleteCommand = new RelayCommand<DictInfo>((item) => Delete(item!));
        SaveCommand = new RelayCommand(async () => await SaveAsync());

        InitSearch();
    }

    #region 属性绑定

    private List<NameValue> _dictCategoryList = DictManager.CategoryDropdownList;
    public List<NameValue> DictCategoryList
    {
        get => _dictCategoryList;
        set => SetProperty(ref _dictCategoryList, value);
    }

    private DictInfo? _dictInfo0;
    public DictInfo? DictInfo0
    {
        get => _dictInfo0;
        set => SetProperty(ref _dictInfo0, value);
    }

    private bool _isOpen = false;
    public bool IsOpen
    {
        get => _isOpen;
        set => SetProperty(ref _isOpen, value);
    }

    private bool _isAdding = false;
    public bool IsAdding
    {
        get => _isAdding;
        set => SetProperty(ref _isAdding, value);
    }

    #endregion

    #region 绑定事件

    public ICommand AddCommand { get; }

    public ICommand UpdateCommand { get; }

    public ICommand DeleteCommand { get; }

    public ICommand SaveCommand { get; }

    #endregion

    #region privates

    public void Add()
    {
        DictInfo0 = new();

        IsAdding = true;
        IsOpen = true;
    }

    public void Update(DictInfo item)
    {
        DictInfo0 = item;

        IsAdding = false;
        IsOpen = true;
    }

    public void Delete(DictInfo item)
    {
        Growl.Ask("确定是否要删除？", isConfirmed =>
        {
            if (isConfirmed)
            {
                _dictInfoService.DeleteDictInfo(item.Id);
                DataSourceList?.Remove(item);
                Growl.Info("数据删除成功");
            }
            return true;
        });
    }

    public async Task SaveAsync()
    {
        var dictInfo = DictInfo0;
        if (string.IsNullOrWhiteSpace(dictInfo!.Name))
        {
            Growl.Info("名称不能为空");
            return;
        }
        if (string.IsNullOrWhiteSpace(dictInfo!.Code))
        {
            Growl.Info("代码不能为空");
            return;
        }

        // 更新
        if (!dictInfo!.IsTransient())
        {
            await _dictInfoService.UpdateDictInfoAsync(dictInfo);

            IsOpen = false;
            Growl.Info("数据更新成功");
            return;
        }

        // 新增
        if (string.IsNullOrWhiteSpace(dictInfo!.Category))
        {
            Growl.Info("标签不能为空");
            return;
        }

        var cardInfo0 = await _dictInfoService.AddDictInfoAsync(dictInfo);
        DataSourceList!.Add(cardInfo0);

        IsOpen = false;
        Growl.Info("数据新增成功");
    }

    #endregion

    protected override (IEnumerable<DictInfo> items, long pageCount) OnSearch(int pageIndex, int pageSize)
    {
        return _dictInfoService.GetPaged(QueryFilter, pageIndex, pageSize);
    }
}
